<template>
      <div>
        <div style="margin-top: -90px;margin-bottom: -20px">添加用户</div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <div style="margin-right: 125px;margin-left: 55px">
          <el-form-item label="用户名" prop="uname" >
            <el-input v-model="ruleForm.uname"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd" >
            <el-input v-model="ruleForm.pwd"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="ruleForm.realname"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="phonenumber">
            <el-input v-model="ruleForm.phonenumber"></el-input>
          </el-form-item>
          <el-form-item label="用户类型" prop="utype">
            <el-radio v-model="ruleForm.utype" label="ROLE_1" disabled>普通用户</el-radio>
          </el-form-item>
        </div>
        <el-form-item style="margin-left: -90px">
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import {adduser} from "@/api/user";

export default {
  data() {
    return {
      ruleForm: {
        realname: '',
        uname: '',
        pwd: '',
        phonenumber: '',
        utype: 'ROLE_1'
      },
      rules: {
        uname: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        realname: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
        phonenumber: [{ required: true, message: '请输入电话号码', trigger: 'blur' }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          adduser(this.ruleForm).then(res=>{
            if(res.code==200){
              //注册成功
              this.$message({type:'success',message:res.msg})
            }else{
              //注册失败
              this.$message({type:'success',message:res.msg})
            }
          }).catch(error=>{})
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
